package cn.ffcs.uec.view.action;

import java.text.ParseException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.zkoss.zk.ui.Component;
import org.zkoss.zk.ui.Components;
import org.zkoss.zk.ui.event.Events;
import org.zkoss.zul.Messagebox;

import cn.ffcs.raptornuke.plugin.common.zk.ctrl.BasePortletComposer;
import cn.ffcs.uec.staffAndOutStaff.vo.StaffAndOutStaff;
import cn.ffcs.uec.view.action.bean.UecInnovationEditBean;
import cn.ffcs.uec.view.model.UecInnovation;
import cn.ffcs.uec.view.model.UecStaffAll;
import cn.ffcs.uom.common.util.DateUtil;
import cn.ffcs.uom.common.util.ListboxUtils;
import cn.ffcs.uom.common.util.PubUtil;
import cn.ffcs.uom.common.util.StrUtil;
import cn.ffcs.uom.common.vo.NodeVo;

@Controller
@Scope("prototype")
public class UecInnovationEditComposer extends BasePortletComposer {

	private static final long serialVersionUID = 3086340593438520480L;
	/**
	 * 页面bean
	 */
	private UecInnovationEditBean bean = new UecInnovationEditBean();
	/**
	 * 操作类型
	 */
	private String opType;
	/**
	 * 人员信息
	 */
	private StaffAndOutStaff uecOutStaffInfo;
	/**
	 * 人员信息
	 */
	private UecStaffAll uecStaffAll;
	
	private UecInnovation uecInnovation;


	@Override
	public void doAfterCompose(Component comp) throws Exception {
		super.doAfterCompose(comp);
		Components.wireVariables(comp, bean);
	}

	/**
	 * 初始化窗口
	 * 
	 * @throws ParseException
	 */
	public void onCreate$uecInnovationEditWindow() throws ParseException {
		this.bindCombobox();
		this.bindBean();
	}

	/**
	 * 绑定combobox
	 */
	private void bindCombobox() {
		// //项目角色
		List<NodeVo> nodeVoIshiskeypsnList = new ArrayList<NodeVo>();
		String[][] ishiskeypsn = { { "主创", "主创" }, { "参与", "参与" }};
		for (int i = 0; i < ishiskeypsn.length; i++) {
			NodeVo nodeVo = new NodeVo();
			for (int j = 0; j < 1; j++) {
				nodeVo.setId(ishiskeypsn[i][j]);
				nodeVo.setName(ishiskeypsn[i][j + 1]);
			}
			nodeVoIshiskeypsnList.add(nodeVo);
		}
		ListboxUtils.rendererForEdit(bean.getJointype(), nodeVoIshiskeypsnList);
	}

	/**
	 * bindBean
	 * 
	 * @throws ParseException
	 */
	public void bindBean() throws ParseException {
		opType = (String) arg.get("opType");
		uecStaffAll = (UecStaffAll) arg.get("uecStaffAll");
		if ("add".equals(opType)) {
			this.bean.getUecInnovationEditWindow().setTitle("新增创新成果");
			bean.getUecInnovationEditDiv().setHeight("40%");
		}
		/*if ("view".equals(opType)) {
			this.bean.getUecInnovationEditWindow().setTitle("查看奖励信息");
			uecInnovation = (UecInnovation) arg.get("uecInnovation");
			if (null != uecInnovation) {
				//PubUtil.fillBeanFromPo(uecEnc, bean);
				bean.getButtonDiv().setVisible(false);
				PubUtil.readOnlyComponent(bean.getUecInnovationEditDiv(), true);
			}
		}*/
		if ("edit".equals(opType)) {
			this.bean.getUecInnovationEditWindow().setTitle("编辑创新结果");
			bean.getUecInnovationEditDiv().setHeight("40%");
			uecInnovation = (UecInnovation) arg.get("uecInnovation");
			if (null != uecInnovation) {
				PubUtil.fillBeanFromPo(uecInnovation, bean);
			}
		}
		
	}

	/**
	 * 保存
	 * 
	 * @throws Exception
	 */
	public void onOk() throws Exception {
		String msg = this.checkUacStaffData();
		if (null != msg) {
			Messagebox.show(msg);
			return;
		}
		// 获取登录用户
		//String currentName = PlatformUtil.getCurrentUser().getScreenName();
		if ("add".equals(opType)) {
			uecInnovation =new UecInnovation();
			//PubUtil.fillPoFromBean(bean, uecImportant);
			uecInnovation = new UecInnovation();
			uecInnovation.setPkPsndoc(uecStaffAll.getPkpsndoc());
			uecInnovation.setInnovationName(bean.getInnovationName().getValue());
			if(!"无".equals(bean.getInnovationName().getValue().trim())) {
				uecInnovation.setGettime(bean.getGettime().getValue());
				uecInnovation.setJointype((String)bean.getJointype().getSelectedItem().getValue());
				uecInnovation.setEvaluateName(bean.getEvaluateName().getValue());
				uecInnovation.setAwardsName(bean.getAwardsName().getValue());
				uecInnovation.setGradeName(bean.getGradeName().getValue());
				uecInnovation.setDocumentName(bean.getDocumentName().getValue());
				uecInnovation.setDocumentNum(bean.getDocumentNum().getValue());
			}
			uecInnovation.setRemark(bean.getRemark().getValue());
			uecInnovation.setStatuscd("1000");
			// 创建时间
			uecInnovation.setCreatedate(new Date());//转换结果为yyyy-mm-dd 00:00:00
			uecInnovation.setEffdate(new Date());
			uecInnovation.addOnly();
			Events.postEvent("onOK", this.self, uecInnovation);
			this.onCancel();
			Messagebox.show("数据新增成功！");
		}
		if ("edit".equals(opType)) {
			
			uecInnovation.setInnovationName(bean.getInnovationName().getValue());
			if(!"无".equals(bean.getInnovationName().getValue().trim())) {
				uecInnovation.setGettime(bean.getGettime().getValue());
				uecInnovation.setJointype((String)bean.getJointype().getSelectedItem().getValue());
				uecInnovation.setEvaluateName(bean.getEvaluateName().getValue());
				uecInnovation.setAwardsName(bean.getAwardsName().getValue());
				uecInnovation.setGradeName(bean.getGradeName().getValue());
				uecInnovation.setDocumentName(bean.getDocumentName().getValue());
				uecInnovation.setDocumentNum(bean.getDocumentNum().getValue());
			}
			uecInnovation.setRemark(bean.getRemark().getValue());
			uecInnovation.setModifyname(uecStaffAll.getPkpsndoc());
			uecInnovation.setStatuscd("1000");
			// 修改时间
			uecInnovation.setUpdatedate(new Date());
			uecInnovation.updateOnly();
			Events.postEvent("onOK", this.self, uecInnovation);
			this.onCancel();
			Messagebox.show("更新成功！");
		}

	}

	/**
	 * 取消
	 */
	public void onCancel() {
		// 关闭窗口
		this.bean.getUecInnovationEditWindow().onClose();
	}

	/**
	 * 检测填入的参数是否为空
	 */
	private String checkUacStaffData() {
		if (StrUtil.isNullOrEmpty(bean.getInnovationName().getValue())) {
			return "请填写项目名称";
		}
		if(!"无".equals(bean.getInnovationName().getValue().trim())) {
			 if (StrUtil.isNullOrEmpty(bean.getGettime().getValue())) {
				return "填写获得年份";
			} else if (StrUtil.isNullOrEmpty(bean.getJointype().getSelectedItem().getValue())) {
				return "填写参与类型";
			} else if (StrUtil.isNullOrEmpty(bean.getGradeName().getValue())) {
				return "填写等级";
			}
			else if (StrUtil.isNullOrEmpty(bean.getEvaluateName().getValue())) {
				return "填写评定单位";
			}  else if (StrUtil.isNullOrEmpty(bean.getAwardsName().getValue())) {
				return "填写获奖奖项";
			}
			else if (StrUtil.isNullOrEmpty(bean.getDocumentName().getValue())) {
				return "填写获奖文件名称";
			}
			else if (StrUtil.isNullOrEmpty(bean.getDocumentNum().getValue())) {
			   return "填写文号";
	        } 
			//字数限制
			if(getLength(bean.getInnovationName().getValue().trim())>60) {
				return "项目名称超出字数限制";
			}
		}
		return null;
	}
	public static int getLength(String str) {
		int templ=0;
		templ=str.length();
		return templ;
	}

}
